package com.example.gzysmartcity.viewmodel;

import android.util.Log;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ThreadUtils;
import com.example.gzysmartcity.repository.model.Person;
import com.example.gzysmartcity.repository.model.PersonBuyInfo;
import com.example.gzysmartcity.repository.model.PersonExpend;
import com.example.gzysmartcity.repository.model.PersonInInfo;
import com.example.gzysmartcity.repository.remote.State;
import com.example.gzysmartcity.utils.NetworkUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class CustomViewModel extends ViewModel {
    private final MutableLiveData<State> customLiveData = new MutableLiveData<>();
    private List<Person.DataBean> mPersonList;
    private List<PersonBuyInfo.DataBean> mPersonBuyInfoList;
    private List<PersonExpend> mPersonExpendList;
    private List<PersonExpend> mPersonExpendListForSearch;
    private List<PersonInInfo.DataBean> mPersonInInfoList;

    public void getPersonData() {
        ThreadUtils.executeByCached(new ThreadUtils.Task<Person>() {
            @Override
            public Person doInBackground() throws Throwable {
                return NetworkUtil.getInstance().get("Person/getAll?", null, Person.class, 0);
            }

            @Override
            public void onSuccess(Person result) {
                mPersonList = result.getData();
                getPersonInInfosData();

            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onFail(Throwable t) {

            }
        });
    }

    public void getPersonInInfosData() {
        ThreadUtils.executeByCached(new ThreadUtils.Task<PersonInInfo>() {
            @Override
            public PersonInInfo doInBackground() throws Throwable {
                return NetworkUtil.getInstance().get("PersonInInfo/getAll?", null, PersonInInfo.class, 0);
            }

            @Override
            public void onSuccess(PersonInInfo result) {
                mPersonInInfoList = result.getData();
                getPersonBuyInfoData();
            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onFail(Throwable t) {

            }
        });
    }


    public void getPersonBuyInfoData() {
        ThreadUtils.executeByCached(new ThreadUtils.Task<PersonBuyInfo>() {
            @Override
            public PersonBuyInfo doInBackground() throws Throwable {
                return NetworkUtil.getInstance().get("PersonBuyInfo/getAll?", null, PersonBuyInfo.class, 0);
            }

            @Override
            public void onSuccess(PersonBuyInfo result) {
                mPersonBuyInfoList = result.getData();
                getPersonExpendsData();
            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onFail(Throwable t) {

            }
        });
    }

    public void getPersonExpendsData() {
        ThreadUtils.executeByCached(new ThreadUtils.SimpleTask<Object>() {
            @Override
            public Object doInBackground() {
                mPersonExpendList = new ArrayList<>();
                for (Person.DataBean person : mPersonList) {
                    String name = person.getName();
                    String pic = person.getPic();
                    List<PersonInInfo.DataBean> ptime = mPersonInInfoList.stream().filter(a -> a.getPersonId() == person.getId()).
                            sorted(Comparator.comparing(PersonInInfo.DataBean::getTime).reversed()).collect(Collectors.toList());
                    int money = (int) mPersonBuyInfoList.stream().filter(a -> a.getPersonId() == person.getId()).mapToDouble(PersonBuyInfo.DataBean::getNum).sum();
                    mPersonExpendList.add(new PersonExpend(name, pic, ptime.get(0).getTime(), money + ""));
                }
                mPersonExpendListForSearch = new ArrayList<>(mPersonExpendList);
                return null;
            }

            @Override
            public void onSuccess(Object result) {
                customLiveData.postValue(State.SUCCESS);
            }
        });
    }

    public void search(String name) {
        mPersonExpendList.clear();
        LogUtils.i(name);
        if (name.equals("")) {
            mPersonExpendList.addAll(mPersonExpendListForSearch);
        } else {
            mPersonExpendList.addAll(mPersonExpendListForSearch.stream().filter(a -> a.getName().contains(name)).collect(Collectors.toList()));
        }
    }

    public MutableLiveData<State> getCustomLiveData() {
        return customLiveData;
    }

    public List<PersonExpend> getPersonExpendList() {
        return mPersonExpendList;
    }

}
